<?php
namespace app\index\controller\project;

use app\index\controller\Base;

class Index extends base_project
{
    public $tableName = "Project";
    public function index()
    {
        $project=model("Project")->where(["id"=>$this->base_project_id])->find();

        $project_plan=model("ProjectPlan")->where(["project_id"=>$this->base_project_id])->with("project")->select();


        $project_team=model("Team")->where(["project_id"=>$this->base_project_id])->with(["user","project"])->select();

         //项目类型
         $project_type=model("CodeMap")->get_value("project_type");
         $project_status=model("CodeMap")->get_value("project_status");
        $data["project_type"]= array_column($project_type,"m_name","m_value");
        $data["project_status"]= array_column($project_status,"m_name","m_value");
        $code_status = model("CodeMap")->get_value("project_plan_status");
        $data["code_status"] = array_column($code_status, "m_name", "m_value");
            //业主单位
            $data["yz_list"]  =model("Company")->where(["c_type"=>6])->select()->toArray();
            //总包单位
            $data["zb_list"]=   model("Company")->where(["c_type"=>1])->select()->toArray();

            $data["pm_info"]=model("admin")->where(["id"=>$project->pm_uid])->find();
            $data["market_info"]=model("admin")->where(["id"=>$project->market_id])->find();

            $data["yz_list"]  = array_column($data["yz_list"],"name","id");
            //总包单位
            $data["zb_list"]=   array_column($data["zb_list"],"name","id");
          
            $data["project_plan"]=  $project_plan;
            $data["project_team"]=  $project_team;
            $data["project"]=  $project;
         
         $this->assign($data);
        return view();
    }

    public function info()
    {
        $project=model("Project")->where(["id"=>$this->base_project_id])->find();

        $project_plan=model("ProjectPlan")->where(["project_id"=>$this->base_project_id])->with("project")->select();


        $project_team=model("Team")->where(["project_id"=>$this->base_project_id])->with(["user","project"])->select();

         //项目类型
         $project_type=model("CodeMap")->get_value("project_type");
         $project_status=model("CodeMap")->get_value("project_status");
        $data["project_type"]= array_column($project_type,"m_name","m_value");
        $data["project_status"]= array_column($project_status,"m_name","m_value");
        $code_status = model("CodeMap")->get_value("project_plan_status");
        $data["code_status"] = array_column($code_status, "m_name", "m_value");
            //业主单位
            $data["yz_list"]  =model("Company")->where(["c_type"=>6])->select()->toArray();
            //总包单位
            $data["zb_list"]=   model("Company")->where(["c_type"=>1])->select()->toArray();

            $data["pm_info"]=model("admin")->where(["id"=>$project->pm_uid])->find();
            $data["market_info"]=model("admin")->where(["id"=>$project->market_id])->find();

            $data["yz_list"]  = array_column($data["yz_list"],"name","id");
            //总包单位
            $data["zb_list"]=   array_column($data["zb_list"],"name","id");
          
            $data["project_plan"]=  $project_plan;
            $data["project_team"]=  $project_team;
            $data["project"]=  $project;
         
         $this->assign($data);
        return view();
    }


    /**
     *  添加
     */
    public function add()
    {
        $model = model('Project');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();

            $data["title"] = $post["title"];
            $data["p_type"] = $post["p_type"];
            $data["company_id"] = $post["company_id"];
            $data["pm_uid"] = $post["pm_uid"];
            $data["market_id"] = $post["market_id"];
            $data["pact_time"] = $post["pact_time"];
            $data["day"] = $post["day"];
  
            $data["status"] = 1;
            $data["owner_id"] = $post["owner_id"];
            $data["price"] = $post["price"];

            $ProjectModel = model("Project");
            $result = $ProjectModel->save($data);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Index/index"));
        }
        $data["users"]=model("admin")->where(["role_id"=>array("NEQ",1)])->select();
        //项目类型
        $data["code_list"]=model("CodeMap")->get_value("project_type");
         //业主单位
         $data["yz_list"]  =model("Company")->where(["c_type"=>6])->select();
        //总包单位
        $data["zb_list"]=   model("Company")->where(["c_type"=>1])->select();
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $ProjectModel = model('Project');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
            $data["id"] = $post["id"];
            $data["title"] = $post["title"];
            $data["p_type"] = $post["p_type"];
            $data["company_id"] = $post["company_id"];
            $data["pm_uid"] = $post["pm_uid"];
            $data["market_id"] = $post["market_id"];
            $data["pact_time"] = $post["pact_time"];
            $data["day"] = $post["day"];
            $data["create_time"] = $post["create_time"];
            $data["update_time"] = $post["update_time"];
            $data["delete_time"] = $post["delete_time"];
            $data["status"] = $post["status"];
            $data["owner_id"] = $post["owner_id"];
            $data["price"] = $post["price"];

            $map["id"] = $post["id"];
            $result = $ProjectModel->save($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/Project/Index/index"));
        }

        $id = input("id");

        $data["info"] = $ProjectModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $ProjectModel = model('Project');
        $result = $ProjectModel->where('id', intval($id))->delete();
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
